package com.dyh.quickdemozwei;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import sun.misc.Unsafe;

import java.lang.reflect.Field;


@Slf4j
public class TestInterruptMonitor {


    Object[] out = new Object[1];


    public void f()   {
        Thread t= new Thread(()->{
            try {
                Thread.sleep(2000L);
            } catch (InterruptedException e) {
                log.info("sleep InterruptedException");
            }
            out[0]="daw";
        }
        );
        t.start();

        try {
            t.join();
        } catch (InterruptedException e) {
            log.info("join InterruptedException");

        }

    }



    class RadicalThread extends Thread{


        Thread monitorThread;

        public RadicalThread() {
            this.monitorThread = new Thread(()->{
                for (;;){
//                    log.info("{}",this);
                    //radical
                    this.interrupt();
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            monitorThread.start();
        }

        @Override
        public void run() {
            log.info("s1");
            f();
            log.info("s2");
        }
    }


    @Test public void test() throws InterruptedException {
        RadicalThread radicalThread = new RadicalThread();
        radicalThread.setName("radicalThread");
        radicalThread.start();
        Thread.sleep(1000L);
        log.info("out={}",out[0]);
        Thread.sleep(2100L);
        log.info("out={}",out[0]);
        radicalThread.join();
        log.info("joined");
    }
}
